local weiyi = fk.CreateSkill{
  name = "hanqing__weiyi",
}

Fk:loadTranslationTable{
  ["hanqing__weiyi"] = "委蛇",
  [":hanqing__weiyi"] = "出牌阶段限一次，你可以重复重铸一张牌，直到因此获得<font color='red'>♥</font>牌，然后蓄谋任意张因此进入弃牌堆的锦囊牌。"..
  "你可以将一个区域内均为<font color='red'>♥</font>牌的所有牌当【无懈可击】使用。",

  ["#hanqing__weiyi"] = "委蛇：你可以将以下一个区域所有牌当【无懈可击】使用",
  ["#hanqing__weiyi-recast"] = "委蛇：你可以重铸一张牌，直到摸到<font color='red'>♥</font>牌",
  ["#hanqing__weiyi-premeditate"] = "委蛇：你可以蓄谋其中的锦囊牌",
  ["hanqing__weiyi_recast"] = "重铸",

  ["$hanqing__weiyi1"] = "臣受公子厚恩，岂有丝毫异心？",
  ["$hanqing__weiyi2"] = "谋定后动，公子稍安勿躁……",
  ["$hanqing__weiyi3"] = "老朽昏聩，诸事当握于我手！",
  ["$hanqing__weiyi4"] = "祸福无妄，唯我善谋者司之！",
  ["$hanqing__weiyi5"] = "王后有诏，诛逆贼黄歇！",
}

local U = require "packages/utility/utility"

weiyi:addEffect("viewas", {
  mute = true,
  pattern = ".",
  prompt = function (self, player, selected_cards, selected)
    if self.interaction.data == nil then
      return "#hanqing__weiyi-recast"
    else
      return "#hanqing__weiyi"
    end
  end,
  interaction = function(self, player)
    if #player:getViewAsCardNames(weiyi.name, {"nullification"}) > 0 then
      local choices = {}
      local mapper = {
        h = "$Hand",
        e = "$Equip",
        j = "$Judge",
      }
      for _, flag in ipairs({"h", "e", "j"}) do
        if #player:getCardIds(flag) > 0 and
          table.every(player:getCardIds(flag), function (id)
            return Fk:getCardById(id).suit == Card.Heart
          end) then
            table.insert(choices, mapper[flag])
        end
      end
      return UI.ComboBox {choices = choices}
    end
  end,
  card_filter = function (self, player, to_select, selected)
    if self.interaction.data == nil then
      return #selected == 0
    else
      return false
    end
  end,
  view_as = function(self, player, cards)
    local card = Fk:cloneCard("nullification")
    card.skillName = weiyi.name
    if self.interaction.data ~= nil then
      if #cards > 0 then return end
      if self.interaction.data == "$Hand" then
        card:addSubcards(player:getCardIds("h"))
      elseif self.interaction.data == "$Equip" then
        card:addSubcards(player:getCardIds("e"))
      elseif self.interaction.data == "$Judge" then
        card:addSubcards(player:getCardIds("j"))
      end
    else
      if #cards ~= 1 then return end
      card = Fk:cloneCard("hanqing__weiyi_recast")
      card:addFakeSubcards(cards)
    end
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    if self.interaction.data ~= nil then
      room:notifySkillInvoked(player, weiyi.name, "control")
      player:broadcastSkillInvoke(weiyi.name, math.random(3, 4))
    else
      room:notifySkillInvoked(player, weiyi.name, "drawcard")
      player:broadcastSkillInvoke(weiyi.name, math.random(2))
      room:setPlayerMark(player, "hanqing__weiyi-phase", 1)
      local all_cards = table.simpleClone(use.card.fake_subcards)
      local card = room:recastCard(all_cards, player, weiyi.name)
      while #card > 0 and Fk:getCardById(card[1]).suit ~= Card.Heart and not player.dead and not player:isNude() do
        card = room:askToCards(player, {
          min_num = 1,
          max_num = 1,
          include_equip = true,
          skill_name = weiyi.name,
          prompt = "#hanqing__weiyi-recast",
          cancelable = true,
        })
        if #card == 0 then
          break
        end
        table.insertTableIfNeed(all_cards, table.simpleClone(card))
        card = room:recastCard(card, player, weiyi.name)
      end
      all_cards = table.filter(all_cards, function (id)
        return Fk:getCardById(id).type == Card.TypeTrick and table.contains(room.discard_pile, id)
      end)
      while #all_cards > 0 and not player.dead and not table.contains(player.sealedSlots, Player.JudgeSlot) do
        local cards = room:askToCards(player, {
          min_num = 1,
          max_num = 1,
          include_equip = false,
          skill_name = weiyi.name,
          pattern = tostring(Exppattern{ id = all_cards }),
          prompt = "#hanqing__weiyi-premeditate",
          cancelable = true,
          expand_pile = all_cards,
        })
        if #cards > 0 then
          U.premeditate(player, cards, weiyi.name, player)
          all_cards = table.filter(all_cards, function (id)
            return Fk:getCardById(id).type == Card.TypeTrick and table.contains(room.discard_pile, id)
          end)
        else
          return ""
        end
      end
      return ""
    end
  end,
  enabled_at_play = function (self, player)
    return player:getMark("hanqing__weiyi-phase") == 0
  end,
  enabled_at_response = function (self, player, response)
    if not response then
      for _, flag in ipairs({"h", "e", "j"}) do
        if #player:getCardIds(flag) > 0 and
          table.every(player:getCardIds(flag), function (id)
            return Fk:getCardById(id).suit == Card.Heart
          end) then
          return #player:getViewAsCardNames(weiyi.name, { "nullification" }, player:getCardIds(flag)) > 0
        end
      end
    end
  end,
  enabled_at_nullification = function (self, player, data)
    for _, flag in ipairs({"h", "e", "j"}) do
      if #player:getCardIds(flag) > 0 and
        table.every(player:getCardIds(flag), function (id)
          return Fk:getCardById(id).suit == Card.Heart
        end) then
        return #player:getViewAsCardNames(weiyi.name, { "nullification" }, player:getCardIds(flag)) > 0
      end
    end
  end,
})

weiyi:addEffect(fk.CardUsing, {
  can_refresh = function (self, event, target, player, data)
    return target == player and data.card.is_damage_card and
      (player.general == "liyuan" or player.deputyGeneral == "liyuan") and
      table.find(data.tos, function (p)
        return p.general == "huangxie" or p.deputyGeneral == "huangxie"
      end)
  end,
  on_refresh = function (self, event, target, player, data)
    player:broadcastSkillInvoke(weiyi.name, 5)
  end,
})

weiyi:addAI(nil, "vs_skill")

return weiyi
